FROM ubuntu:20.04

LABEL author ssct-rust-team <yanyanyan@smail.nju.edu.cn>

ENV DEBIAN_FRONTEND=noninteractive

RUN apt-get update

# 安装Python 3.8和pip
RUN apt-get install -y python3.8 python3-pip python3.8-venv


# 设置 Python 虚拟环境
RUN python3.8 -m venv /opt/c2saferust-venv && \
    /opt/c2saferust-venv/bin/pip install --upgrade pip

# 安装 Python 依赖
COPY requirements.txt /tmp/requirements.txt
RUN /opt/c2saferust-venv/bin/pip install -r /tmp/requirements.txt

# 安装 curl
RUN apt-get install -y curl

# 设置 Rust 镜像源
ENV RUSTUP_DIST_SERVER=https://mirrors.ustc.edu.cn/rust-static
ENV RUSTUP_UPDATE_ROOT=https://mirrors.ustc.edu.cn/rust-static/rustup

# 安装Rust编译器和工具链
RUN curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -y && \
    . "$HOME/.cargo/env"

# 将Rust的bin目录添加到PATH环境变量中
ENV PATH="/root/.cargo/bin:$PATH"


# 将 clang+llvm-14.0.0-x86_64-linux-gnu-ubuntu-18.04.tar.xz 复制到容器中的 /tmp 目录
COPY clang+llvm-14.0.0-x86_64-linux-gnu-ubuntu-18.04.tar.xz /tmp/

WORKDIR /tmp
# 解压并设置 Clang 环境
RUN xz -d clang+llvm-14.0.0-x86_64-linux-gnu-ubuntu-18.04.tar.xz 
RUN tar -xf clang+llvm-14.0.0-x86_64-linux-gnu-ubuntu-18.04.tar
RUN mv clang+llvm-14.0.0-x86_64-linux-gnu-ubuntu-18.04 clang-llvm-14.0.0
RUN mkdir /usr/lib/llvm-14
RUN cp -r clang-llvm-14.0.0/* /usr/lib/llvm-14/

# 激活 Clang 环境
RUN echo 'export PATH=/usr/lib/llvm-14/bin:$PATH' >> ~/.bashrc && \
    bash -c "source ~/.bashrc"


# 将整个项目复制到容器中
COPY . /workspace

# 设置工作目录
WORKDIR /workspace/Tool

# 数据预处理
RUN /opt/c2saferust-venv/bin/python3 c_files_preprocess.py ../Input/01-Primary/test ../Input/01-Primary/src:../Input/01-Primary/test Intermediate-Result-test
RUN /opt/c2saferust-venv/bin/python3 c_files_preprocess.py ../Input/01-Primary/src ../Input/01-Primary/src:../Input/01-Primary/test Intermediate-Result

# 编译静态和动态分析工具
RUN cd blockfinder && cargo build && cd ..
RUN cd cargopubadder && cargo build && cd ..
RUN cd codeextractor && cargo build && cd .. 
RUN cd contextextractor && cargo build && cd ..
RUN cd fix-unclosed-delimiter && cargo build && cd ..
RUN cd merge-impl && cargo build && cd ..
RUN cd remove-funcdecl && cargo build && cd .. 
RUN cd symbol-table && cargo build && cd ..

